package com.GamersCom.listener;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.GamersCom.db.Platform;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.PlatformMgr;

/**
 * Application Lifecycle Listener implementation class Listener
 *
 */
public class Listener implements ServletContextListener {

	private ServletContext context = null;
	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent event) {
    	context = event.getServletContext();
    	
		//UserDAO userDao = new UserDAO();
    	//DAOFactory factory = new DAOFactory();
		//context.setAttribute("daoFactory", factory);
    	MgrFactory factory = new MgrFactory();
    	context.setAttribute("mgrFactory", factory);
		//set the application root
    	String root = context.getContextPath();
    	context.setAttribute("root", root);
		
    	PlatformMgr pfMgr = factory.getPlatformMgr();
    	try
		{
			List<Platform> gameSubMenu = pfMgr.getAll();
			context.setAttribute("gameSubMenu", gameSubMenu);
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent event) {
    	context = event.getServletContext();
        
        if (context != null) 
        {
        	context.removeAttribute("daoFactory");
        }
        
    }
}
